import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../service/http.service';
import { HttpRouterService } from '../../service/http-router.service';
import { StorageService } from '../../service/storage.service';
import { Router, ActivatedRoute } from '@angular/router';
import _ from 'underscore';

@Component({
  selector: 'app-order-details',
  templateUrl: './order-details.component.html',
  styleUrls: ['./order-details.component.less']
})
export class OrderDetailsComponent implements OnInit {

  // public danDetail = {
  //   realname: '',
  //   money: '',
  //   mobile: '',

  //   age: '',
  //   region_name: '',

  //   occupation: '',
  //   workinglife: '',
  //   socialsecurity: '',
  //   accumulationfund: '',
  //   payrolltype: '',
  //   monthlyincome: '',

  //   insurance: '',
  //   linecard: '',
  //   housetype: '',
  //   cartype: ''
  // };
  public detail = [];

  constructor(
              private _http: HttpService,
              private _storage: StorageService,
              private _activatedRoute: ActivatedRoute,
              private _httpRouter: HttpRouterService,
              private _router: Router
            ) { }

  ngOnInit() {
    const param = 'id=' + this._storage.getSession('danId');
    this._http.post(this._httpRouter.userDetail, param).then(res => {
      if  ( res.code === 1) {
        this.detail = res.data;
        this.detail['mobile'] = this.detail['mobile'].substr(0, 3) + '****' + this.detail['mobile'].substr(7);
        this.detail['age'] = this.age(this.detail['birthday']);
      }
    });
  }

  age(str) {
    // const r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    // const d = new Date(r[1], r[3] - 1, r[4]);
    // if (d.getFullYear() === r[1] && (d.getMonth() + 1) === r[3] && d.getDate() === r[4]) {
    //       const Y = new Date().getFullYear();
    //       return Y - r[1];
    //     }
    const birList = [parseInt(str.substr(0, 4), 0), parseInt(str.substr(5, 7), 0), parseInt(str.substr(8, 10), 0)];
    const now = new Date();
    const newList = [now.getFullYear(), now.getMonth() + 1, now.getDate()];
    if (birList[0] < newList[0]) {
      if (birList[1] < newList[1]) {
        return newList[0] - birList[0];
      }else if (birList[1] === newList[1]) {
        if (birList[2] <= newList[2]) {
          return newList[0] - birList[0];
        }else {
          return newList[0] - birList[0] - 1;
        }
      }else {
        return newList[0] - birList[0] - 1;
      }
    }else {
      return 0;
    }
  }

}
